package ac.svn.accesscontrol.ui.helper;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;

import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.store.ISVNItem;

public class LocationButtonActionListener implements ActionListener {


	private JButton      targetButton 	= null;
	private JComponent dataSourceView   = null;
	private String          messageId 	= null;
	private String     messageParamId	= null;

	public LocationButtonActionListener(JButton targetButt, String messageId,String paramId)
	{
		this.targetButton 	= targetButt;
		this.messageId 		= messageId;
		this.messageParamId 	= paramId;
	}


	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(null != targetButton)
		{
			NotificationEvent notificationEvent = new NotificationEvent(messageId);
			getParameterData(notificationEvent);
			NotificationRegistrar.dispatchNotification(notificationEvent);
		}
	}
	/***
	 * Method is point where logic related to message parameter can be filled 
	 * @return
	 */
	private void getParameterData(NotificationEvent notificationEvent)
	{
		if(null != dataSourceView)
		{
			if(dataSourceView instanceof JList)  
			{
				JList dataSource = (JList) dataSourceView;
				//Check if parameter Id is mentioned.
				if((null != messageParamId) && (false == messageParamId.trim().isEmpty()))
				{
					ISVNItem selectedItem = (ISVNItem) dataSource.getSelectedValue();
					if(null != selectedItem)
					{
						notificationEvent.addParameter(messageParamId,selectedItem);
						notificationEvent.addParameter(ISVNIdentifier.P_AUTOMATIC,Boolean.TRUE);
					}
				}
			}
			else
			{
				//Do nothing
			}
		}
		else
		{
			//Do nothing
		}
	}


	public JButton getTargetButton() {
		return targetButton;
	}


	public void setTargetButton(JButton targetButton) {
		this.targetButton = targetButton;
	}


	public JComponent getDataSourceView() {
		return dataSourceView;
	}


	public void setDataSourceView(JComponent dataSourceView) {
		this.dataSourceView = dataSourceView;
	}


	public String getMessageId() {
		return messageId;
	}


	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}


	public String getMessageParamId() {
		return messageParamId;
	}


	public void setMessageParamId(String messageParamId) {
		this.messageParamId = messageParamId;
	}
}
